<?php

require_once 'Processing/Classes/Application.php';
require_once 'Processing/Classes/TimeGroup.php';
require_once 'Processing/Classes/InformationSystemGroup.php';

/**
 * Group application data to is.
 * 
 * @link https://docs.google.com/drawings/d/14j1dcTZkUgCa9MAxL90maDSMCHrWQrcpkxXTWGf79qk/edit?hl=lt
 * @status TESTED in (rev123), NOT_APPROVED
 * @author Aurelijus Banelis
 */
class ISData {

    /**
     * @param array $is_time_groups [is][date]=>InformationSystemGroup
     *              changes are written to given array
     * @todo move to is processing
     * @todo do not repeat yourself (copied from predict(&$time_groups))
     */
    public static function predict_is(&$is_time_groups) {
        if (count($is_time_groups > 0)) {
            /* Getting predicted dates */
            //TODO: optimise, DRY principle
            $first_is = reset($is_time_groups);
            $first_date = null;
            $last_not_predicted = null;
            foreach ($first_is as $date => $time_group) {
                if (!$first_date) {
                    $first_date = $date;
                }
                if ($time_group->predicted) {
                    $predicted_dates[] = $date;
                } else {
                    $last_not_predicted = $date;
                }
            }

            /* Predicting */
            foreach ($is_time_groups as $is => $row) {
                $last_date = $last_not_predicted;
                foreach ($predicted_dates as $date) {
                    /* Counting average */
                    $back_date = $date;
                    $historical_used = array();
                    $historical_time = array();
                    $n = 0;
                    do {
                        $back_year = intval(substr($back_date, 0, 4)) - 1;
                        $back_date = $back_year . substr($back_date, 4);
                        if (strtotime($back_date) > strtotime($first_date) && array_key_exists($back_date, $row)) {
                            $historical_administrated[] = $row[$back_date]->sum_administrated();
                            $historical_time[] = $row[$back_date]->sum_time_used();
                            $n++;
                        }
                    } while (strtotime($back_date) > strtotime($first_date));
                    if ($n) {
                        $avg_used = array_sum($historical_used) / $n;
                        $avg_time = array_sum($historical_time) / $n;
                        $latest_used = reset($historical_used);
                        $latest_time = reset($historical_time);
                        $used = round(($avg_used + $latest_used) / 2);
                        $row[$date]->sum_n += $used;
                        $time = round(($avg_time + $latest_time) / 2);
                        $row[$date]->sum_time += $time;
                    } else {
                        $row[$date]->sum_n += $row[$last_date]->sum_n;
                        $row[$date]->sum_time += $row[$last_date]->sum_time;
                    }

                    /* Last date */
                    $last_date = $date;
                }
            }
        }
    }
}

?>